// /sw.js
self.addEventListener('install', (event) => {
    const cacheKey = 'MyCache_v2';
  
    event.waitUntil(caches.open(cacheKey).then((cache) => {
      return cache.addAll([
        '/css/global.css',
        '/js/main.js',
        '1.jpg'
      ]);
    }));
  });

  self.addEventListener('activate', (event) => {
    // Specify allowed cache keys
    const cacheAllowList = ['MyCache_v2'];
  
    event.waitUntil(caches.keys().then((keys) => {
      // Delete all caches that aren't in the allow list:
      return Promise.all(keys.map((key) => {
        console.log("service-worker清除旧缓存")
        if (!cacheAllowList.includes(key)) {
          return caches.delete(key);
        }
      }));
    }));
  });